#include "gamepage.h"
#include "ui_gamepage.h"
#include <QPainter>
#include <QPen>
#include <QButtonGroup>
#include "systemtools.h"
#include <QMouseEvent>
#include <QDebug>
#include <QTimer>
#include <QTime>
#include "aboutpage.h"
#include "helppage.h"

GamePage * GamePage::gp = nullptr;

GamePage::GamePage(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::GamePage)
{
    ui->setupUi(this);
    //设置尺寸
    this->setFixedSize(800, 600);
    //隐藏标题栏、边框
    this->setWindowFlags(
                    Qt::FramelessWindowHint|
                    Qt::WindowMinimizeButtonHint);
    //背景透明
    this->setAttribute(Qt::WA_TranslucentBackground);
    //设置标题
    this->setWindowTitle("打地鼠");
    this->setWindowIcon(QIcon(":/images/gamelogo.png"));
    //初始化老鼠组
    mouseInit();
    //移动老鼠位置
    mouseMove();
    //初始化随机数种子
    srand(static_cast<unsigned int>(QTime::currentTime().toString("z").toInt()));
    //游戏定时器初始化
    gameTimer = new QTimer;
    gameTimer->setInterval(500);
    connect(gameTimer,&QTimer::timeout,
            this,&GamePage::gameTimerTimeoutSlot);
    //计时定时器初始化
    timeTimer = new QTimer;
    timeTimer->setInterval(1000);
    connect(timeTimer,&QTimer::timeout,
            this,&GamePage::timeTimerTimeoutSlot);
}

GamePage::~GamePage()
{
    delete ui;
}

GamePage *GamePage::getObject()
{
    if(gp == nullptr)
        gp = new GamePage;
    return gp;
}

void GamePage::gameStart()
{
    //判断当前状态
    if(gameStat == 1)   //游戏已经开始
        return;
    //停止状态 复位数据
    if(gameStat == 0){
        ui->lcdNumberGrade->display(0);
        if(gameMode == 0)
            ui->lcdNumberTime->display(60);
        else
            ui->lcdNumberTime->display(100);
    }
    //暂停状态 回复动作
    else{
        for(int i = 0; i < 9; i++){
            qobject_cast<MyMouseButton *>(mouseGroup->button(i))->mouseStart();
        }
    }
    //启动定时器
    gameTimer->start();
    timeTimer->start();
    //标记状态
    gameStat = 1;
}

void GamePage::gamePause()
{
    //判断当前状态，非游戏状态则直接返回
    if(gameStat != 1)
        return;
    //停止定时器
    gameTimer->stop();
    timeTimer->stop();
    //标记状态
    gameStat = 2;
    //老鼠暂停
    for(int i = 0; i < 9; i++){
        qobject_cast<MyMouseButton *>(mouseGroup->button(i))->mousePause();
    }
}

void GamePage::gameStop()
{
    //判断当前状态
    if(gameStat == 0)
        return;
    //停止定时器
    gameTimer->stop();
    timeTimer->stop();
    //标记当前状态
    gameStat = 0;
    //强制老鼠消失
    for(int i = 0; i < 9; i++){
        qobject_cast<MyMouseButton *>(mouseGroup->button(i))->mouseHide();
    }
}

void GamePage::mouseClickedSlot()
{
    //判断游戏状态
    if(gameStat != 1)
        return;
    MyMouseButton *mouse = qobject_cast<MyMouseButton *>(sender());
    //判断老鼠状态
    if(!mouse->mouseIsActive())
        return;
    if(mouse->getMouseIsClicked())
        return;
    //得分
    int type = mouse->getMouseType();
    int temp = 0;
    switch (type) {
    case 0:
        temp = 5;
        break;
    case 1:
        temp = 10;
        break;
    case 2:
        temp = -5;
        break;
    case 3:
        temp = 20;
        break;
    }
    //显示新的分数
    ui->lcdNumberGrade->display(ui->lcdNumberGrade->intValue() + temp);
    //老鼠回洞
    mouse->hitMouse();
}

void GamePage::gameTimerTimeoutSlot()
{
    //生成新的老鼠
    //选择老鼠洞
    int index = rand() % 9;
    //选择分值
    int type = rand() % 10;
    switch (type) {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
        qobject_cast<MyMouseButton *>(mouseGroup->button(index))
                ->setMouseType(0);
        break;
    case 5:
    case 6:
    case 7:
        qobject_cast<MyMouseButton *>(mouseGroup->button(index))
                ->setMouseType(1);
        break;
    case 8:
        qobject_cast<MyMouseButton *>(mouseGroup->button(index))
                ->setMouseType(2);
        break;
    case 9:
        qobject_cast<MyMouseButton *>(mouseGroup->button(index))
                ->setMouseType(3);
        break;
    }
    //激活选中老鼠
    qobject_cast<MyMouseButton *>(mouseGroup->button(index))->mouseActive();
    //如果是计数模式，将数量减一
    if(gameMode == 1){
        int newTime = ui->lcdNumberTime->intValue() - 1;
        ui->lcdNumberTime->display(newTime);
        if(newTime <= 0){
            gameStop();
        }
    }
}

void GamePage::timeTimerTimeoutSlot()
{
    //刷新时间
    //判断当前状态
    if(gameMode != 0)
        return;
    //显示的秒数减一
    int newTime = ui->lcdNumberTime->intValue() - 1;
    ui->lcdNumberTime->display(newTime);
    if(newTime <= 0){
        gameStop();
    }
}

void GamePage::paintEvent(QPaintEvent *)
{
    //画师 QPainter
    //画笔 QPen
    //画布 this
    QPainter painter;
    QPen pen;
    QPixmap pixmap;
    pixmap.load(":/images/gamepage_background.png");
    //开始绘图
    painter.begin(this);
    //设置画笔
    painter.setPen(pen);
    //绘制图片
    painter.drawPixmap(0,0,this->width(), this->height(),pixmap);
    //结束绘图
    painter.end();
}

void GamePage::mousePressEvent(QMouseEvent *event)
{
    //判断点击坐标y
    if(event->y() > 150)
        return;
    //记录起始坐标
    oldPos = event->pos();
    //开始移动
    isMove = true;
}

void GamePage::mouseReleaseEvent(QMouseEvent *)
{
    //停止移动
    isMove = false;
}

void GamePage::mouseMoveEvent(QMouseEvent *event)
{
    //判断当前是否允许移动
    if(!isMove)
        return;
    //移动
    this->move(this->pos() + event->pos() - oldPos);
}

void GamePage::mouseInit()
{
    //按钮组初始化
    mouseGroup = new QButtonGroup;
    mouseGroup->addButton(ui->btnMouse_1, 0);
    mouseGroup->addButton(ui->btnMouse_2, 1);
    mouseGroup->addButton(ui->btnMouse_3, 2);
    mouseGroup->addButton(ui->btnMouse_4, 3);
    mouseGroup->addButton(ui->btnMouse_5, 4);
    mouseGroup->addButton(ui->btnMouse_6, 5);
    mouseGroup->addButton(ui->btnMouse_7, 6);
    mouseGroup->addButton(ui->btnMouse_8, 7);
    mouseGroup->addButton(ui->btnMouse_9, 8);
    //绑定按钮信号
    for(int i = 0; i < 9; i++){
        connect(mouseGroup->button(i),&MyMouseButton::clicked,
                this,&GamePage::mouseClickedSlot);
    }
}

void GamePage::mouseMove(int mapIndex)
{
    //0 默认地图的坐标组
    if(mapIndex == 0)
    {
        mouseGroup->button(0)->move(170, 211);
        mouseGroup->button(1)->move(370, 209);
        mouseGroup->button(2)->move(579, 215);
        mouseGroup->button(3)->move(140, 311);
        mouseGroup->button(4)->move(371, 311);
        mouseGroup->button(5)->move(583, 310);
        mouseGroup->button(6)->move(130, 416);
        mouseGroup->button(7)->move(370, 420);
        mouseGroup->button(8)->move(605, 424);
    }
}

void GamePage::on_btnExit_clicked()
{
    //退出程序
    SystemTools::quitApp();
}

void GamePage::on_btnStart_clicked()
{
    gameStart();
}

void GamePage::on_btnStop_clicked()
{
    gameStop();
}

void GamePage::on_btnPause_clicked()
{
    gamePause();
}

void GamePage::on_btnAbout_clicked()
{
    //切换界面
    HelpPage::getObject()->hide();
    AboutPage::getObject()->show();
}

void GamePage::on_btnHelp_clicked()
{
    //切换界面
    AboutPage::getObject()->hide();
    HelpPage::getObject()->show();
}

void GamePage::on_btnChangeMode_clicked()
{
    //判断游戏状态
    if(gameStat != 0)
        return;
    if(gameMode == 0){//当前为计时模式
        //切换到计数模式
        gameMode = 1;
        //ui模式按钮显示老鼠
        ui->btnChangeMode->setStyleSheet("border-image: url(:/images/mousemode.png);");
    }
    else{//当前为计数模式
        //切换到计时模式
        gameMode = 0;
        //ui模式按钮显示计时器
        ui->btnChangeMode->setStyleSheet("border-image: url(:/images/timemode.png);");
    }
}
